package com.ruoyi.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.result.Result;
import com.ruoyi.domain.T09;
import com.ruoyi.domain.T21;
import com.ruoyi.domain.T21Vo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.mapper.T18Mapper;
import com.ruoyi.service.IT21Service;
import com.ruoyi.service.impl.T09ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 企业知识库版本Controller
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/T21")
public class T21Controller extends BaseController
{
    @Autowired
    private IT21Service t21Service;
    @Autowired
    private T18Mapper t18Mapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private T09ServiceImpl t09ServiceImpl;

    /**
     * 查询企业知识库版本列表
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:list')")
    @GetMapping("/list")
    @ApiOperation("获取当前存在的版本")
    public TableDataInfo list(T21 t21,HttpServletRequest request)
    {
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        Long CompId = t18Mapper.selectF0301ByF1801(userId);
        t21.setF0301(CompId);
        List<T21> list = t21Service.selectT21List(t21);
        return getDataTable(list);
    }

    /**
     * 导出企业知识库版本列表
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:export')")
    @Log(title = "企业知识库版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, T21 t21)
    {
        List<T21> list = t21Service.selectT21List(t21);
        ExcelUtil<T21> util = new ExcelUtil<T21>(T21.class);
        util.exportExcel(response, list, "企业知识库版本数据");
    }

    /**
     * 获取企业知识库版本详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:query')")
    @GetMapping(value = "/{F2101}")
    public AjaxResult getInfo(@PathVariable("F2101") Long F2101)
    {
        return success(t21Service.selectT21ByF2101(F2101));
    }

    /**
     * 新增企业知识库版本
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:add')")
    @Log(title = "企业知识库版本", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody T21 t21)
    {
        return toAjax(t21Service.insertT21(t21));
    }


    @PostMapping("/addActivity")
    @Log(title = "企业知识库版本", businessType = BusinessType.INSERT)
    public AjaxResult addActivity(@RequestBody T21Vo t21Vo, HttpServletRequest request)
    {
        try {
            T21 t21 = new T21();
            copyT21Fields(t21Vo, t21);
            if(!ObjectUtils.isEmpty(t21Vo.getVersion())){
                Long version = t21Vo.getVersion();
                return t21Service.addActivitys(t21,request,version);
            }
            return t21Service.addActivity(t21,request);
        } catch (Exception e) {
            return AjaxResult.error("添加失败");
        }
    }

    public  void copyT21Fields(T21Vo source, T21 target) {
        Field[] fields = T21.class.getDeclaredFields(); // 获取 T21 类的所有字段
        for (Field field : fields) {
            field.setAccessible(true); // 确保可以访问私有字段
            try {
                Object value = field.get(source); // 从 T21Vo 获取字段值
                field.set(target, value); // 将值设置到新的 T21 对象中
            } catch (IllegalAccessException e) {
                e.printStackTrace(); // 处理异常
            }
        }
    }

    @GetMapping("/toProcessPass")
    @ApiOperation("审批通过")
    public AjaxResult toProcessPass(HttpServletRequest request,@RequestParam("F2101") Long F2101)
    {
       return  t21Service.toProcessPass(request,F2101);
    }


    @GetMapping("/toProcessOut")
    @ApiOperation("审批失败")
    public AjaxResult toProcessOut(HttpServletRequest request,@RequestParam("F2101") Long F2101)
    {
        return  t21Service.toProcessOut(request,F2101);
    }

    /**
     * 修改企业知识库版本
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:edit')")
    @Log(title = "企业知识库版本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody T21 t21)
    {
        System.out.println(t21.toString());
        return toAjax(t21Service.updateT21(t21));
    }

    /**
     * 删除企业知识库版本
     */
//    @PreAuthorize("@ss.hasPermi('system:T21:remove')")
    @Log(title = "企业知识库版本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{F2101s}")
    public AjaxResult remove(@PathVariable Long[] F2101s)
    {
        return toAjax(t21Service.deleteT21ByF2101s(F2101s));
    }


    @GetMapping("/changeVerson")
    @ApiOperation("切换版本")
    public AjaxResult changeVerson(HttpServletRequest request,@RequestParam("F2101") Long F2101)
    {
        t21Service.changeVerson(request,F2101);
        return success();
    }
    @GetMapping("/getT21ByF2107")
    @ApiOperation("通过F2107获取T21")
    public Result getT21ByF2107(@RequestParam("F2107") String F2107)
    {
        T21 t21 = t21Service.selectT21ByF2107(F2107);
        List<Long> F0901s = JSON.parseArray(t21.getF2106(), Long.class);
        List<T09> list = new ArrayList<>();
        for(Long f0901:F0901s){
            T09 t09 = t09ServiceImpl.selectT09ByF0901(f0901);
            list.add(t09);
        }
        t21.setT09List(list);
        return Result.success(t21,"查询成功");
    }

    @GetMapping("/getApproveList")
    public TableDataInfo getApproveList(T21 t21,HttpServletRequest request)
    {
        startPage();
        t21.setF2108(2L);
        List<T21> list = t21Service.selectT21List(t21);
        return getDataTable(list);
    }

}
